﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Huirui.Cavan.Core.MyLinq;
using Huirui.Cavan.Core.Persistent;
using Huirui.Cavan.Domain.MainBoundedContext.SearchModule;
using Huirui.Cavan.Core.Extension;

namespace Huirui.Cavan.Domain.MainBoundedContext.StockModule
{
    public class StockSpecification : ISpecification<Stock, StockSearch>
    {
        #region ISpecification<Stock,StockSearch> 成员

        public StockSearch SearchParams
        {
            get;
            set;
        }

        #endregion

        #region ISpecification<Stock> 成员

        public System.Linq.Expressions.Expression<Func<Stock, bool>> Expression
        {
            get 
            {
                IQueryBuilder<Stock> queryBuilder = QueryBuilder.Create<Stock>();

                if (SearchParams.IsNotNull())
                {
                    if (SearchParams.ID.IsNotEmpty())
                    {
                        queryBuilder = queryBuilder.Equals(s => s.Id, SearchParams.ID);
                    }
                    if (SearchParams.Name.IsNotEmpty())
                    {
                        queryBuilder = queryBuilder.Equals(s => s.Name, SearchParams.Name);
                    }
                    if (SearchParams.Nature > 0)
                    {
                        queryBuilder = queryBuilder.Equals(s => s.Nature, SearchParams.Nature);
                    }
                    if (SearchParams.StockTypeID > 0)
                    {
                        queryBuilder = queryBuilder.Equals(s => s.Type.Id, SearchParams.StockTypeID);
                    }
                    if (SearchParams.BarCode.IsNotEmpty())
                    {
                        queryBuilder = queryBuilder.Equals(s => s.BarCode, SearchParams.BarCode);
                    }
                }

                return queryBuilder.Expression.Expand();
            }
        }

        #endregion
    }
}
